<?php

namespace App\Containers\AppSection\User\UI\API\Requests;

use App\Ship\Parents\Requests\Request;
use App\Ship\Rules\PhoneRule;
use Illuminate\Validation\Rule;

class RegisterUserWithProfileRequest extends Request
{
    /**
     * Define which Roles and/or Permissions has access to this request.
     */
    protected array $access = [
        'permissions' => '',
        'roles' => '',
    ];

    /**
     * Id's that needs decoding before applying the validation rules.
     */
    protected array $decode = [

    ];

    /**
     * Defining the URL parameters (`/stores/999/items`) allows applying
     * validation rules on them and allows accessing them like request data.
     */
    protected array $urlParameters = [

    ];

    public function rules(): array
    {
        return [
            'email' => ['required', 'email', Rule::unique('users', 'email')],
            'email_code' => ['required', 'digits:' . config('appSection-confirmation.code-length')],
            'phone' => ['required', new PhoneRule, Rule::unique('profiles', 'phone')],
            'phone_code' => ['required', 'digits:' . config('appSection-confirmation.code-length')],
            'role' => ['required', Rule::in(['investor', 'borrower'])],
            'legal_form_type' => ['required',
                Rule::in($this->getInputByKey('role') === 'borrower' ? ['ie', 'entity'] : ['ie', 'indiv', 'entity'])
            ],
            'without_patronymic' => ['required', 'boolean'],
            'fio' => [Rule::requiredIf($this->getInputByKey('legal_form_type') !== 'entity'), 'max:100'],
            'company' => [Rule::requiredIf($this->getInputByKey('legal_form_type') === 'entity'), 'max:300'],
            'password' => ['required', 'min:8', 'max:255'],
            'confidentiality_acceptance' => ['required', Rule::in([true, 1, '1'])],
        ];
    }

    public function authorize(): bool
    {
        return $this->check([
            'hasAccess',
        ]);
    }
}
